﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CNCSoft.EData;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using System.Threading;
using System.Globalization;

using Common;

namespace CNCSoft.ErpApps.TMS.Quantri
{
    public partial class frmTinhHinhDongHocPhi : Form
    {
        public frmTinhHinhDongHocPhi()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }

        DataSet ds = new DataSet();

        private void frmTinhHinhDongHocPhi_Load(object sender, EventArgs e)
        {
            txtMaNienHoc.Text = GlobalVariable.WSInfo.getAppProperty("MaNienHocDefault");
            
            //Load DM_HOCPHI_DOT cho cbo
            DataTable dt = new DataTable();
            SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_HOCPHI_DOT");
            this.cboHocPhi_Dot.DataSource = dt;
            this.cboHocPhi_Dot.DisplayMember = "TenHocPhi_Dot";
            this.cboHocPhi_Dot.ValueMember = "MaHocPhi_Dot";

            cboHocPhi_Dot.SelectedIndex = -1;
            
            gridView1.Columns.ColumnByFieldName("Cơ sở").Group();
            gridView1.Columns.ColumnByFieldName("Lớp").Group() ;
            gridView1.Columns.ColumnByFieldName("Tình trạng HP").Group();
        }


        private void GetData(string strDotHP)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                SqlCommand sqlCom = new SqlCommand("sp_QuanTri_TinhHinhDongHocPhi", cnn);
                sqlCom.CommandType = CommandType.StoredProcedure;

                sqlCom.Parameters.Add(new SqlParameter("@pMaHocPhi", SqlDbType.VarChar)).Value = strDotHP;
                sqlCom.Parameters.Add(new SqlParameter("@pMaNienHoc", SqlDbType.VarChar)).Value = txtMaNienHoc.Text;

                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom);

                if (ds.Tables["DANHSACH"] != null)
                    ds.Tables["DANHSACH"].Clear();

                dataAdapter.Fill(ds, "DANHSACH");
                grid_DanhSach.DataSource = ds.Tables["DANHSACH"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdInDS_Click(object sender, EventArgs e)
        {
            try
            {
                cmdInDS.Enabled = false;
                BindingManagerBase bmb = this.BindingContext[ds, "DANHSACH"];

                //Load report
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\Report_TuyenSinh_DSHocSinhDaDongHocPhi.rpt");

                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);

                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTuyenSinh"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaNienHoc", txtMaNienHoc.Text);
                //PublicFunction.SetParameter(rpt, "@pMaHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["GiaTriLoc"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaHocPhi", cboHocPhi_Dot.SelectedValue.ToString());
                PublicFunction.SetParameter(rpt, "pTenDotHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["TenHocPhi_Dot"].ToString());
                PublicFunction.SetParameter(rpt, "pMoRongDong", false);

                //View report
                frmPrintPreview frmPP = new frmPrintPreview(rpt);
                frmPP.ShowDialog();
                frmPP.Dispose();

                rpt.Dispose();
                cmdInDS.Enabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        private void cboHocPhi_Dot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboHocPhi_Dot.SelectedIndex == -1 || cboHocPhi_Dot.SelectedValue.ToString() == "" || cboHocPhi_Dot.Items.Count<=0) return;

            GetData(cboHocPhi_Dot.SelectedValue.ToString());
        }

        private void txtMaNienHoc_TextChanged(object sender, EventArgs e)
        {

        }

        private void cmdInDSChuaDongHP_Click(object sender, EventArgs e)
        {
            try
            {
                cmdInDS.Enabled = false;
                BindingManagerBase bmb = this.BindingContext[ds, "DANHSACH"];

                //Load report
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\Report_TuyenSinh_DSHocSinhDaDongHocPhi_ChuaDong.rpt");

                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);

                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTuyenSinh"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaNienHoc", txtMaNienHoc.Text);
                //PublicFunction.SetParameter(rpt, "@pMaHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["GiaTriLoc"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaHocPhi", cboHocPhi_Dot.SelectedValue.ToString());
                PublicFunction.SetParameter(rpt, "pTenDotHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["TenHocPhi_Dot"].ToString());
                PublicFunction.SetParameter(rpt, "pMoRongDong", false);

                //View report
                frmPrintPreview frmPP = new frmPrintPreview(rpt);
                frmPP.ShowDialog();
                frmPP.Dispose();

                rpt.Dispose();
                cmdInDS.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdDSDaDongHp_Click(object sender, EventArgs e)
        {
            try
            {
                cmdInDS.Enabled = false;
                BindingManagerBase bmb = this.BindingContext[ds, "DANHSACH"];

                //Load report
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\Report_TuyenSinh_DSHocSinhDaDongHocPhi_DaDong.rpt");

                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);

                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTuyenSinh"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaNienHoc", txtMaNienHoc.Text);
                //PublicFunction.SetParameter(rpt, "@pMaHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["GiaTriLoc"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaHocPhi", cboHocPhi_Dot.SelectedValue.ToString());
                PublicFunction.SetParameter(rpt, "pTenDotHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["TenHocPhi_Dot"].ToString());
                PublicFunction.SetParameter(rpt, "pMoRongDong", false);

                //View report
                frmPrintPreview frmPP = new frmPrintPreview(rpt);
                frmPP.ShowDialog();
                frmPP.Dispose();

                rpt.Dispose();
                cmdInDS.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdHoanTatHocPhi_Click(object sender, EventArgs e)
        {
            try
            {
                cmdHoanTatHocPhi.Enabled = false;
                
                //Load report
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\Report_QuanTri_HoanTatHocPhi.rpt");

                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);

                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTuyenSinh"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaNienHoc", txtMaNienHoc.Text);
                //PublicFunction.SetParameter(rpt, "@pMaHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["GiaTriLoc"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaHocPhi", cboHocPhi_Dot.SelectedValue.ToString());
                PublicFunction.SetParameter(rpt, "pTenDotHocPhi", ((DataTable)cboHocPhi_Dot.DataSource).Rows[cboHocPhi_Dot.SelectedIndex]["TenHocPhi_Dot"].ToString());
                PublicFunction.SetParameter(rpt, "pMoRongDong", false);

                //View report
                frmPrintPreview frmPP = new frmPrintPreview(rpt);
                frmPP.ShowDialog();
                frmPP.Dispose();

                rpt.Dispose();
                cmdHoanTatHocPhi.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        
    }
}
